import { DeleteOutlined, EditOutlined } from '@ant-design/icons';
import { Space, Button, Tooltip } from "antd";
import { useEffect } from "react";
import { useTable } from "@/hooks/useTable";
import { useFormDialog } from "@/hooks/useForm";
import CloudTable from "@/component/CloudTable";
import { page, del, batDel, save, edit, findDetail } from "@/views/attendance/work/api";
import { STATUS_ENUMS, STATUS_OPTIONS, WEEK_ENUMS } from "@/views/attendance/work/data";
import WorkForm from './form/WorkForm';

const Work = () => {

    // -------------------
    // ---   表单操作   ---
    // -------------------

    // 表单提交
    const onWorkFormCallback = (model) => {
        model?.detail?.forEach(each => {
            each.workday = WEEK_ENUMS[each.number] || null;
        });
        workForm.onSubmit(model, workTable.onReload);
    }

    // 表单设置
    const workForm = useFormDialog({
        width: 500,
        save: save,
        edit: edit,
        callback: onWorkFormCallback,
    });

    // -------------------
    // ---   表格操作   ---
    // -------------------

    // 表格自定义按钮
    const workTools = () => {
        return (
            <Space>
                <Button type="primary" onClick={() => workForm.isOpenDialog(undefined, '新增')}>新增</Button>
                <Button type='dashed' style={{ color: 'red' }} onClick={() => workTable.onBatchRemove(workTable.selected)}>删除</Button>
            </Space>
        );
    }

    // 表格列
    const workColumns = [
        {
            title: '#',
            key: 'index',
            width: 50,
            align: 'center',
            fixed: 'left',
            render: (val, record, index) => {
                return index + 1;
            }
        },
        {
            title: '名称',
            dataIndex: 'name',
            key: 'name',
            width: 'auto',
            ellipsis: true,
            search: 'default',
        },
        {
            title: '状态',
            dataIndex: 'status',
            key: 'status',
            width: 'auto',
            search: 'selected',
            options: STATUS_OPTIONS(),
            render: (val) => STATUS_ENUMS[val] || ''
        },
        {
            title: '操作',
            dataIndex: 'id',
            width: 100,
            align: 'center',
            fixed: 'right',
            render: (val, row) => {
                return (
                    <div>
                        <Tooltip title="编辑">
                            <Button type="text" size="small" icon={<EditOutlined />} style={{ margin: '0 4px', color: 'blue' }} onClick={() => onEditWorkForm(row)} />
                        </Tooltip>
                        <Tooltip title="删除">
                            <Button type="text" size="small" icon={<DeleteOutlined />} style={{ margin: '0 4px', color: 'red' }} onClick={() => workTable.onRemove(val)} />
                        </Tooltip>
                    </div>
                )
            }
        }
    ];

    // 表格设置
    const workTable = useTable({
        reload: page,
        remove: del,
        batchRemove: batDel,
        tools: workTools,
        columns: workColumns,
    });

    // -------------------
    // ---   方法事件   ---
    // -------------------

    const onEditWorkForm = (row) => {
        let query = {
            params: [{
                field: 'systemId',
                value: row.id
            }]
        }
        findDetail(query).then(resp => {
            let model = {
                ...row,
                detail: resp?.data || [],
            }
            workForm.isOpenDialog(model, '编辑');
        })

    }

    useEffect(() => {
        workTable.onReload();
    }, []);

    return (
        <>
            <CloudTable props={workTable} />
            <WorkForm props={workForm} />
        </>
    );
}

export default Work;